package com.aiflow.Action;

import it.sauronsoftware.base64.Base64;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.aiflow.util.MD5;

public class CallTel {

	public Logger log = Logger.getLogger(CallTel.class);
	public static Properties properties;
	public final int batchSize = 3;
	public int count = 20;
	// 是否接听
	public boolean flaglisten = false;
	public boolean flag = false;

	public SimpleDateFormat sdftoday = null;
	public SimpleDateFormat sdftime = null;
	public Base64 base64 = null;
	public String ip = null;
	public String port = null;
	public String webkey = null;
	public String context = null;
	public String exten = null;
	public String priority = null;
	public String callerid = null;
	public String variable = null;
	public String async = null;
	public String json = null;
	public String waiting = null;
	public HttpURLConnection httpurl = null;
	public HttpURLConnection httpurlticket = null;

	public StringBuilder sb = null;
	public String line = "";
	public int waittime = 1;
	public String date1 = null;

	public String phone[] = null;
	public String MVB_CALLER = null;
	public SimpleDateFormat sdf2 = null;
	public String sendContext = null;

	// 即时拨打(根据手机号和内容来拨打)
	public void callTell(String phonenum, String ttstext) throws IOException {
		MD5 md = new MD5();
		String path = "";// this.getClass().getResource("/").getPath();
		path = this.getClass().getProtectionDomain().getCodeSource()
				.getLocation().getPath();
		path = path.substring(0, path.indexOf("/WEB-INF")).replace("%20", " ");
		File file = new File(path + "/cfg.properties");

		Properties properties = new Properties();
		InputStream is = new FileInputStream(file);

		properties.load(is);

		ip = properties.getProperty("ip");
		port = properties.getProperty("port");
		webkey = properties.getProperty("webkey");
		context = properties.getProperty("context");
		exten = properties.getProperty("exten");
		priority = properties.getProperty("priority");
		callerid = properties.getProperty("callerid");
		variable = properties.getProperty("variable");
		async = properties.getProperty("async");
		json = properties.getProperty("json");

		Base64 base64 = new Base64();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		MVB_CALLER = sdf.format(new Date());
		sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 生成当前的webkey
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = sdf1.format(new Date());
		webkey = md.encryptMD5(webkey + date1);
		// base64加密
		try {
			sendContext = base64.encode(ttstext);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String phone[] = phonenum.split(",");

		int phoneLen = phone.length;
		int phonec = phoneLen / this.count;// 83
		int phonem = phoneLen % this.count;
		int lastlength = phonec * 2;

		for (int j = 0; j < phonec; j++) {
			for (int i = 0; i < this.count; i++) {
				String sjh = phone[j * this.count + i];
				new callTellThread(sjh, this).start();
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for (int i = lastlength; i < phoneLen; i++) {
			String sj = phone[i];
			new callTellThread(sj, this).start();
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// String sjh =
		// "13971152727,13995605729,15872356966,15802796796,15072476459,13720159013,13554451549,13476171810,13419648230";
		//
		// CallTel call = new CallTel();
		// try {
		// call.callTell(sjh, "防盗告警(老数据库接口并行)");
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		String j = "12，df,fg，对方";
		String[] k = j.split("[,，，，，，，，]");

		System.out.println(k[0]);
		System.out.println(k[1]);
		System.out.println(k[2]);
		System.out.println(k[3]);
	}

}
